package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.TaskExcelModel;
import com.yupi.springbootinit.model.entity.Task;
import com.yupi.springbootinit.model.vo.TaskReq;
import com.yupi.springbootinit.service.TaskService;
import com.yupi.springbootinit.mapper.TaskMapper;
import com.yupi.springbootinit.utils.DateFormatUtils;
import com.yupi.springbootinit.utils.LongSeqGenerator;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【t_task(任务表)】的数据库操作Service实现
 * @createDate 2024-11-26 17:06:09
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task>
        implements TaskService {


    public static final String 志嘉 = "志嘉";
    @Autowired
    private TaskMapper taskMapper;
    public static final String JIRA_URL = "http://jira.trustlife.com/browse/";

    @Override
    public List<Task> list() {
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("valid", "1");
        return super.list(queryWrapper);
    }

    @Override
    public List<Task> listByReq(Date date, String version) {
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("valid", "1");
        if (date != null) {
            queryWrapper.like("version", DateFormatUtils.yyyyMM(date));
        }
        if (StringUtils.isNotBlank(version)) {
            queryWrapper.eq("version", version);
        }
        return super.list(queryWrapper);
    }

    public Task getTaskByCode(String taskCode) {
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("valid", "1");

        if (StringUtils.isNotBlank(taskCode)) {
            queryWrapper.eq("task_code", taskCode);
        }
        List<Task> list = super.list(queryWrapper);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    @Transactional
    public void updateByTaskCode(TaskReq req) {
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("valid", "1");
        queryWrapper.eq("task_code", req.getTaskCode());
        Task one = this.getOne(queryWrapper);
        Task extracted = taskConvert(req);
        if (one == null) {
            throw new BusinessException("未找到对应任务");
        }
        extracted.setVersion(one.getVersion());
        this.remove(queryWrapper);
        this.save(extracted);
    }

    private Task taskConvert(TaskReq req) {
        Task task = new Task();
        task.setId(LongSeqGenerator.generate());
        task.setTaskName(req.getTaskName());
        String taskCode = req.getTaskCode();
        task.setTaskCode(taskCode);
        task.setTaskUrl(getTaskUrl(taskCode));
        task.setTaskDesc("");
        task.setVersion(req.getVersion());
        task.setIsSql(req.getIsSql());
        task.setIsDms(req.getIsDms());
        task.setStatus(req.getStatus());
        Double plan = req.getPlan();
        task.setPlan(plan == null ? 0 : plan);
        task.setEnvironment(req.getEnvironment());
        task.setCreateTime(new Date());
        task.setUpdateTime(new Date());
        task.setValid(1);
        return task;
    }

    @Override
    public List<String> getTaskVersion() {
        return taskMapper.getTaskVersion();
    }


    @Override
    public void saveTask(List<TaskExcelModel> stringListMap) {
        List<Task> taskList = stringListMap.stream()
                .filter(x -> StringUtils.isNotBlank(x.getExec()))
                .filter(x -> x.getExec().contains(志嘉))
                .map(x -> getTask(x)).collect(Collectors.toList());
        for (Task task : taskList) {
            Task taskByCode = getTaskByCode(task.getTaskCode());
            if (taskByCode == null) {
                this.save(task);
            }
        }
    }


    private Task getTask(TaskExcelModel x) {
        Task task = new Task();
        String taskCode = x.getTaskCode();
        task.setId(LongSeqGenerator.generate());
        task.setTaskName(x.getTaskName());
        task.setTaskCode(taskCode);
        task.setTaskUrl(getTaskUrl(taskCode));
        task.setTaskDesc("");
        task.setVersion(StringUtils.isBlank(x.getVersion()) ? "暂未指定" : x.getVersion());
        task.setIsSql("0");
        task.setIsDms("0");
        task.setStatus(0);
        task.setPlan(0D);
        task.setEnvironment("开发中");
        task.setCreateTime(new Date());
        task.setUpdateTime(new Date());
        task.setValid(1);
        return task;
    }

    private static @NotNull String getTaskUrl(String taskCode) {
        return JIRA_URL + taskCode;
    }

}




